lappend auto_path .

package require logger
set log [logger::init harness]
logger::import -namespace logger harness


#setenv TCLLIBPATH /some_path
source ./system.tcl
#source .//harness.tcl


array set env [list TEST_PATH /some_path ]
array set env [list TEST_EXEC /some_exec_path ]
array set env [list LOCAL_HOME [pwd]]
array set env [list REGRESSO_BASE /some_path ]

set REGRESSSO_BASE $env(REGRESSO_BASE)
set TEST_EXEC      $env(TEST_EXEC)
set TEST_PATH      $env(TEST_PATH)
set LOCAL_HOME     $env(LOCAL_HOME)

#-----------------------------------------------
# do_unit
#-----------------------------------------------

proc do_unit {LOCAL_HOME TEST_PATH TCL} {
    global env
    logger::info "... Running $TCL ..."
    set TEST_EXEC $env(TEST_EXEC)
    set tcl_path $TEST_PATH/$TCL
    # 1. copy & fix the tcl
    cd $LOCAL_HOME
    logger::debug "1. copy & fix tcl $tcl_path"
    file copy -force $TEST_PATH/$TCL $LOCAL_HOME
    # 2. make sandbox
    set dir_sandbox $LOCAL_HOME/$TCL.sandbox
    logger::debug "2. make sandbox $dir_sandbox"
    file mkdir $dir_sandbox
    cd $dir_sandbox
    # 3. execute
    logger::debug "3. execution"
    set timeout 500000
    set exec_cmd "$TEST_EXEC -no_home_init -f ../$TCL > ../$TCL.log"
    system::exec_with_timeout $timeout $exec_cmd
    logger::debug "4. process golden log"
    # 4. assert
    set dir_assert $LOCAL_HOME/$TCL.assert
    file mkdir $dir_assert
    cd $dir_assert
    # 4.1 process r:log   r=reference
    interp create assert_r
    assert_r eval {
	source ./harness.tcl
	source ./iomux.tcl
    }
    assert_r eval [list array set harness::meta::context [list r:file $tcl_path.log]]
    assert_r eval {
	set file [lindex [array get harness::meta::context r:file] 1]
        #parse_log r $file
    }
    interp delete assert_r
    logger::debug "5. process run log"
    # 4.2 process i:log   i=implementation
    interp create assert_i
    assert_i eval {
	source ./harness.tcl
	source ./iomux.tcl
    }
    assert_i eval [list harness::meta::context set i:file ../$TCL.log]
    assert_i eval [list harness::meta::context set r:sandbox $TEST_PATH/$TCL.sandbox]
    assert_i eval {
	set file [harness::meta::context get i:file]
        parse_log i $file
    }
    interp delete assert_i
    # 6. diff i:cp r:cp
}

#------------------------------------------------------------
set tcl_files [glob $TEST_PATH/*.tcl]

foreach tcl_path $tcl_files {
    set tcl [file tail $tcl_path]
    do_unit $LOCAL_HOME $TEST_PATH $tcl
}


